<?php

namespace app\controller;

use app\BaseController;
use app\model\Site;
use think\facade\Db;
use think\facade\Session;
use app\utils\Utils;
use think\facade\View;
use app\utils\DataCache;
use app\model\User;

class Message extends BaseController
{
    static public function index()
    {

        $email = Session::get('email', '');
        $pw = Session::get('password', '');
        $my_user_info = Utils::user($email, $pw);

        $list =  Db::connect()->table('site_message')->where('user_id', $my_user_info['id'])->select()->toArray(); // 查询数据

        return View::fetch(Site::get('template') . '/message', [
            'list' => $list
        ]);
    }

    static public function get_message()
    {
        $code = 200;

        $email = Session::get('email', '');
        $pw = Session::get('password', '');
        $my_user_info = Utils::user($email, $pw);
        $user_id = $my_user_info['id'];

        if ($user_id == '') {
            // 未登录
            $code = 403;
            $msg = '未登录';
        } else {
            $messages = Db::connect()->table('site_message')->where('user_id', $user_id)->select()->toArray(); // 查询发送给自己的信息
            $value = array();
            foreach ($messages as $key) {
                $i = $key['from_user_id'];
                $data_cache = DataCache::read('shequ_get_user|' . $i);
                if ($data_cache != false) {
                    $user_data = $data_cache;
                } else {
                    $user = User::where('id', $i)->select()->toArray(); // 查询用户的数据
        
                    $group = Db::table('site_user_group_user_list')->where('user_id', $i)->select()->toArray();
        
                    if (count($user) == 0) { // 没有数据
                        $user_data = array(
                            'id' => $i,
                            'name' => '该用户已注销',
                            'img' => 'https://pic.imgdb.cn/item/660823079f345e8d03683ae4.png',
                            'group' => $group
                        );
                        DataCache::write('shequ_get_user|' . $i, $user_data, 20);
                    } else {
                        $user_data = array(
                            'id' => $i,
                            'name' => $user[0]['name'],
                            'img' => $user[0]['img'],
                            'group' => $group
                        );
                        DataCache::write('shequ_get_user|' . $i, $user_data, 20);
                    }
                }

                $i = json_decode($key['text'], true)['post_id'];

                $data_cache = DataCache::read('shequ_get_post|' . $i);
                if ($data_cache != false) {
                    $post_data = $data_cache;
                } else {
                    $posts = Db::connect()->table('site_post')->where('id', $i)->select()->toArray(); // 查询帖子的数据
                    if (count($posts) == 0) { // 没有数据
                        $post_data = array(
                            'title' => '该帖子已注销'
                        );

                    } else {
                        $post_data = array(
                            'title' => $posts[0]['title']
                        );
                    }
                }
                if (json_decode($key['text'], true)['type'] == 'talk'){
                    $value[] = array(
                        "id" => $key['id'],
                        "type" => json_decode($key['text'], true)['type'],
                        "user_id" => $key['from_user_id'],
                        "user_name" => $user_data['name'],
                        "user_img" => $user_data['img'],
                        "post_id" => json_decode($key['text'], true)['post_id'],
                        "post_title" => $post_data['title'],
                        "time" => $key['time'],
                        "status" => $key['status'],
                        "talk_text" => json_decode($key['text'], true)['text']
                    );
                }else{
                    $value[] = array(
                        "id" => $key['id'],
                        "type" => json_decode($key['text'], true)['type'],
                        "user_id" => $key['from_user_id'],
                        "user_name" => $user_data['name'],
                        "user_img" => $user_data['img'],
                        "post_id" => json_decode($key['text'], true)['post_id'],
                        "post_title" => $post_data['title'],
                        "time" => $key['time'],
                        "status" => $key['status']
                    );
                }


            }
            $code = 200;
            $msg = '获取成功';
        }
        $re = array(
            "code" => $code,
            'msg' => $msg
        );
        if ($code == 200) {
            $re['data'] = $value;
        }
        return json($re);
    }
}
